Load some packages that we’ll need to use to do these calculations:
library(tidyverse)
library(gifski)
library(ggraph)
library(here)
library(igraph)
library(tnet) # for the closeness function described here: https://toreopsahl.com/2010/03/20/closeness-centrality-in-networks-with-disconnected-components/
source(here("modelFunction_rewiring.R"))
# Define parameters
N = 50
edge.prob <- 0.04
n.removed = 10
burn.in = 20
recovery = 5
add00 = c(0.5, 10)
lose01 = 0.1
add10 = 0.05
lose11 = c(0.5, 0.5)
coefAdd = 0.5
coefLose = -0.5
modelGraphs <- runModel(N = N, # Nodes in the network
edge.prob = edge.prob,
n.removed = n.removed,
burn.in = burn.in,
recovery = recovery,
add00 = add00,
lose01 = lose01,
add10 = add10,
lose11 = lose11,
coefAdd = coefAdd,
coefLose = coefLose)$graphs
## Warning: Removed 1 row(s) containing missing values (geom_path).
First, I run the model 100 times and compute the network measures for each of the model runs.
Now, I can make some plots to detect general trends in what happens to the network after removal/rewiring.
# Time slice numbers for line placement
back1 <- which(names(modelGraphs) == "back1")
removed <- which(names(modelGraphs) == "removed")
rewired <- which(names(modelGraphs) == "rewired")
Density behaves the same no matter the number of individuals in the network, just with less variation. To be expected.
## Warning: Removed 500 row(s) containing missing values (geom_path).
## Removed 500 row(s) containing missing values (geom_path).
Mean distance looks like it might have a nonlinear relationship with the number of individuals in the network. Need to investigate this further!
Modularity seems to behave the same no matter the number of individuals in the network.
## Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if `.name_repair` is omitted as of tibble 2.0.0.
## Using compatibility `.name_repair`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
A few sanity checks:
What about the ratio between the first and second changes? Aka: what percentage of the loss/gain is recovered by the rewiring?
## Warning in log(ratio): NaNs produced
## Warning in log(ratio): NaNs produced
## Warning in log(ratio): NaNs produced
## Warning: Removed 883 rows containing non-finite values (stat_smooth).
## Warning: Removed 862 rows containing missing values (geom_point).
## Warning in log(ratio): NaNs produced
## Warning in log(ratio): NaNs produced
## Warning in log(ratio): NaNs produced
## Warning: Removed 883 rows containing non-finite values (stat_smooth).
## Warning: Removed 862 rows containing missing values (geom_point).
## Warning in log(ratio): NaNs produced
## Warning in log(ratio): NaNs produced
## Warning in log(ratio): NaNs produced
## Warning: Removed 883 rows containing non-finite values (stat_smooth).
## Warning: Removed 862 rows containing missing values (geom_point).
## Warning: Removed 21 rows containing non-finite values (stat_smooth).
## Warning: Removed 21 rows containing non-finite values (stat_smooth).
## Warning: Removed 21 rows containing non-finite values (stat_smooth).